﻿using System;
using System.Collections.Generic;
using System.Text;
using DataProvider;
using System.Data;
using System.Data.SqlClient;

namespace BusinessLayer
{
    public class NhaCC :XL_Bang
    {

        #region Cac thuoc tinh
        private string _maNhaCC;
        public string MaNhaCC
        {
            get { return _maNhaCC; }
            set { _maNhaCC = value; }
        }
        private string _tenNhaCC;
        public string TenNhaCC
        {
            get { return _tenNhaCC; }
            set { _tenNhaCC = value; }
        }
        private string _diaChi;
        public string DiaChi
        {
            get { return _diaChi; }
            set { _diaChi = value; }
        }
        private string _sdt;
        public string SDT
        {
            get { return _sdt; }
            set { _sdt = value; }
        }
        #endregion

        #region cac phuong thuc
        public DataTable GetData()
        {
            StrSQL = "select * from NHACC";
            return GetDataTable();
        }

        public bool AddNew()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "INSERT_UPDATE_DELETE_NHACC";
                command.Parameters.AddWithValue("@MANHACC", _maNhaCC);
                command.Parameters.AddWithValue("@TENNHACC", _tenNhaCC);
                command.Parameters.AddWithValue("@DIACHI", _diaChi);
                command.Parameters.AddWithValue("@DIENTHOAI", _sdt);
                command.Parameters.AddWithValue("@TYPE", 0);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                close();
                return true;
            }
            catch (Exception)
            {
                close();
                return false;
            }
        }
        public bool Edit()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "INSERT_UPDATE_DELETE_NHACC";
                command.Parameters.AddWithValue("@MANHACC", _maNhaCC);
                command.Parameters.AddWithValue("@TENNHACC", _tenNhaCC);
                command.Parameters.AddWithValue("@DIACHI", _diaChi);
                command.Parameters.AddWithValue("@DIENTHOAI", _sdt);
                command.Parameters.AddWithValue("@TYPE", 1);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                close();
                return true;
            }
            catch (Exception)
            {
                close();
                return false;
            }
        }
        public bool Delete()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "INSERT_UPDATE_DELETE_NHACC";
                command.Parameters.AddWithValue("@MANHACC", _maNhaCC);
                command.Parameters.AddWithValue("@TENNHACC", string.Empty);
                command.Parameters.AddWithValue("@DIACHI", string.Empty);
                command.Parameters.AddWithValue("@DIENTHOAI", string.Empty);
                command.Parameters.AddWithValue("@TYPE", 2);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                close();
                return true;
            }
            catch (Exception)
            {
                close();
                return false;
            }
        }

        public DataTable Search(string keyword)
        {
            StrSQL = "select * from Nhacc where manhacc like '%" + keyword + "%' or tennhacc like '%" + keyword + "%' or diachi like '%" + keyword + "%' or dienthoai like '%" + keyword + "%'";
            return GetDataTable();
        }
        public DataTable NhaCungCapCoNhieuDonDHNhat()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "sp_NhaCungCapCoNhieuDonDHNhat";
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable table = new DataTable();
                adapter.Fill(table);
                return table;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                command.Dispose();
                close();
            }
        }
        
        #endregion

      
    }
}
